<?php
$JO[ "Fashion" ] 		= JO(array(
		"EN"=>"JukeOn™ Fashion",
		"VI"=>"Thời Trang JukeOn™"
	) );
$JO["JO_searchid"] 	 	= JO(array("EN"=>"Search Key",			"VI"=>"Search"	));

/*if(!isset ($_SESSION["JO_account_key"]) || $_SESSION["JO_account_authority"]!="SO"){

	echo "<script>location='http://jukeon.vn/JO/';</script>";
}*/
$searchkeyartist=$JO["JO_searchid"];
$wherequery="";
if(isset ($_REQUEST["searchkey"])){
    $searchkeyartist=$_REQUEST["searchkey"];
    if($searchkeyartist!=$JO["JO_searchid"]){
        $wherequery= "and artist.`name` like '%".$searchkeyartist."%'";
    }
}

$page=1;
if(isset ($_REQUEST["Page"])){$page=$_REQUEST["Page"];}
if(isset( $_REQUEST[ "type" ] ) )
$key=$_REQUEST[ "type" ];
if(isset( $_REQUEST[ "key" ] ) ){
$recommendation=$_REQUEST[ "key" ];
		$query="Select * from `recommendation` where `delete` is null and `key`=".$_REQUEST[ "key" ]."";
          
                $recordSet = JO_query( $query );
                if($recordSet->num_rows) while ($record = $recordSet -> fetch_array( ))	  
				{
				$start= $record['start_date'];
				$exp=$record['expired_date'];
				}
	}
				  
?>

<div class="JO_page">
  <h1>
    <?= $JO["Fashion"] ?>
  </h1>
  <h2>&nbsp;</h2>
  <h3>&nbsp;</h3>
</div>
<script type="text/javascript">
    var musics=new Array();
    var n=0;
	
    $(document).ready(function(){  
                                                                            
            //When you click on a link with class of poplight and the href starts with a #   
            $('a.poplight[href^=#]').click(function() {  
                    var popID = $(this).attr('rel'); //Get Popup Name  
                    var popWidth = 480; //Gets the first query string value  
                    //Fade in the Popup and add close button  
                    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="JO_80_image/fileclose.ico" class="btn_close" width="20px" title="Close Window" alt="Close" onclick="location.reload(true)"/></a>');  
                      
                    //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css  
                    var popMargTop = ($('#' + popID).height() + 80) / 2;  
                    var popMargLeft = ($('#' + popID).width() + 80) / 2;  
                      
                    //Apply Margin to Popup  
                    $('#' + popID).css({   
                            'margin-top' : -popMargTop,  
                            'margin-left' : -popMargLeft  
                    });  
                      
                    //Fade in Background  
                    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.  
                    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer   
                      
                    return false;  
            });  
              
              
            //Close Popups and Fade Layer  
            $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...  
                      $('#fade , .popup_block').fadeOut(function() {  
                            $('#fade, a.close').remove();    
            }); //fade them both out  
                      
                    return false;  
            });  
       
              
    });  
    function JO_load_iframe(id){
		if(id==""){
			document.getElementById("JO_iframe1").src="add_iframe.php";
		}else{
			document.getElementById("JO_iframe1").src="add_iframe.php?key="+id;
		}
	}
</script>
<div id="popup_name2" class="popup_block">
  <iframe
			id		= 'JO_iframe1'
			name	= 'JO_iframe1'
			src=""
            style="width:482px;height:480px"
		></iframe>
</div>
<!---->
<div class="JO_relative" style="width: 960px">
  <div class="JO_left" style="width: 240px;">
    <div class="JO_box_240">
      <h1 style="margin-top:0">Artist</h1>
      <form method="post" target="JO_integration_asynchronous" action="JO_05_execution.php" name="f2" >
        <h2><a href="#?w=488" rel="popup_name2" class="poplight" onclick="JO_load_iframe('');" >+</a></h2>
        <h2 style="position:absolute;top:-1px;left:84px" class="tru"><a href="javascript:document.f2.submit();" >--</a></h2>
        <!--hoac dung f2.submit()-->
        
        <table class="left" >
          <?
             $querycount="
			SELECT * FROM recommendation WHERE `delete` is NULL Order by start_date
";
             $recordSetnum = JO_query( $querycount );
             $numpages=$recordSetnum->num_rows;
             if(($numpages%30)!=0) {$numpages=(int)($numpages/30)+1;}
             else                {$numpages=$numpages/30;}
            $query="
          	SELECT * FROM recommendation WHERE `delete` is NULL Order by start_date 
			
            ";
				         $recordSet = JO_query( $query );
             if($recordSet->num_rows) while ($record = $recordSet -> fetch_array( )){
             ?>
          <tr >
            <td ><a href="?JO_unit=68_managing_Top_Rbt1&type=<?=$record["type"]?>&key=<?=$record["key"]?>">
              <?=$record["subject"]?>
              </a></td>
            <td style="padding:0 0 0 4px"><input type="checkbox" name="chb[]" value="<?=$record["key"]?>"/>
              <input type="hidden" name="JO_query"	value="delete_recommendation"/></td>
            <td width="5%" style="padding:0"><a href="#?" rel="popup_name2" class="poplight" onclick="JO_load_iframe('<?=$record["key"]?>');"><img src="JO_80_image/b_edit.png"  style="cursor: pointer;margin:4px" /></a></td>
          </tr>
          <tr> </tr>
          <?
             }
			
             ?>
          <tr >
            <td style="text-align: center;border-bottom: 0px"><?

                if($numpages>1){
                    if($page>2){
                    ?>
              <a href="#" onclick="sendurl('1');">◀</a>
              <?
                    }
                    if($page>1){
                    ?>
              <a href="#" onclick="sendurl('<?=($page-1)?>');">◁</a>
              <?
                    }
                    if($page-4>1) echo "...";
                    for($i=$page-4;$i<=($page-1);$i++){
                        if($i>0){
                        ?>
              <a href="#" onclick="sendurl('<?=$i?>');">
              <?=$i?>
              </a>
              <?
                        }
                    }
                    echo  "<b style='color=#ffffff;'>".$page."</b>";
                    for($i=$page+1;$i<=($page+4);$i++){
                        if($i<=$numpages){
                        ?>
              <a href="#" onclick="sendurl('<?=$i?>');">
              <?=$i?>
              </a>
              <?
                        }
                    }
                    if($page+4<$numpages) echo "...";
                    if($page<$numpages){
                    ?>
              <a href="#" onclick="sendurl('<?=($page+1) ?>');">▷</a>
              <?
                    }
                    if($page<($numpages-1)){
                    ?>
              <a href="#" onclick="sendurl('<?=$numpages ?>');">▶</a>
              <?
                    }

                }
                 ?></td>
          </tr>
        </table>
      </form>
    </div>
  </div>
  <div class="JO_left">
    <div class="JO_bar_720">
      <?
             $query="Select * from `recommendation` where `delete` is null and `key`='".$_REQUEST["key"]."'";
           
                $recordSet = JO_query( $query );
                if($recordSet->num_rows) $record = $recordSet -> fetch_array( )
            ?>
      <h1>
        <?= isset($_REQUEST['key'])? $record["subject"]:"Top OF";$recommendation=$record["key"];?>
      </h1>
      <form action="JO_05_execution.php" method="post" target="JO_integration_asynchronous">
        <span id="tb_top_music">
        <table style="width: 100%;margin: 0px 4px 0px 0px;">
          <tr>
            <th style="width: 50px">Top</th>
            <th><?=($key=="music")?("Top Music"):(($key=="album")?"Top album":"Top Artist")?></th>
            <th style="width: 20PX"></th>
            <th style="width: 20PX"></th>
            <th style="width: 20PX"></th>
          </tr>
        </table>
        <?
               
				if(isset ($_REQUEST["key"])){
				
                    $query="SELECT DISTINCT
" . (	$key=="music" ?  "music.`subject`"  :(($key=="album")?"album.`subject`":"artist.`name`")) . ",
recommended_list.position,
" . (	$key=="music" ?  "music.`key`"  :(($key=="album")?"album.`key`":"artist.`key`")) . "
FROM
recommendation ,
recommended_list ,
" . (	$key=="music" ?  "music"  :(($key=="album")?"album":"artist")) . "
WHERE
recommended_list.`delete` IS NULL AND
recommended_list.key_item = " . (	$key=="music" ?  "music.`key`"  :(($key=="album")?"album.`key`":"artist.`key`")) . " AND
recommended_list.`recommendation` = $recommendation order by `recommended_list`.`position` ASC";
	
                }
                  $recordSet = JO_query( $query );
				  $ntop=1;
               	 if($recordSet->num_rows) while ($record = $recordSet -> fetch_array( )){
				
                    ?>
        <span id='trtop_<?=$record["key"]?>'>
        <input type="hidden" name="key[]" value="<?= $record["key"]?>"/>
        <table  style="width: 100%;margin: 0px 4px 0px 0px;">
          <tr >
            <td style="width: 50px" id='tdtopid_<?=$record["key"]?>'><?=$ntop?></td>
            <td id='tdtop_<?=$record["key"]?>'><?=($key=="artist")?$record["name"]:$record["subject"]?></td>
            <td style="width: 12px"><a href='#i' onclick='topup("<?=$record["key"]?>");'><img src="JO_80_image/JO_27_icon_up_on.png" class="JO_icon" alt="Up" title="Up" /></a></td>
            <td style="width: 12px"><a href='#i' onclick='topdown("<?=$record["key"]?>");'><img src="JO_80_image/JO_28_icon_down_on.png" class="JO_icon" alt="Down" title="Down" /></a></td>
            <td style="width: 12px"><a href='#i' onclick='removetop("<?=$record["key"]?>");'><img src="JO_80_image/JO_23_icon_delete_on.png" class="JO_icon" alt="Remove" title="Remove" /></a></td>
          </tr>
        </table>
        </span> 
        <script type="text/javascript">
                            musics[n]=<?=$record['key']?>;
                            n++;
							
                        </script>
        <?
					 $ntop++;
                 }
            
                ?>
        </span>
        <input type="hidden" value="<?=$recommendation?>" name="recommendation" />
        <input type="hidden" value="<?=$start?>" name="start" />
        <input type="hidden" value="<?=$exp?>" name="exp" />
        <input type="hidden" value="SaveTop" name="JO_query" />
        <input type="submit" class="JO_button" style="width: 100%" value="Save" />
      </form>
      <span id="tb_music" >
      <table style="width: 100%;margin: 0px 4px 0px 0px;">
        <tr>
          <th>Subject</th>
          <th width="410px"> 
          <th></th>
            </th>
          <th>Tìm Kiếm :
            <input type="text" value="<?=$_REQUEST['sea']?>" style="height:12px !important" onkeypress="searchKeyPress(event,this.value)"/></th>
        </tr>
      </table>
      <?
               
                if(isset ($_REQUEST["type"])){
				$key=$_REQUEST["type"];
					if(isset ($_REQUEST["sea"])){
						$sea=$_REQUEST['sea'];
						$querycount="
			select * from $key where `key` not in(
SELECT
recommended_list.key_item
FROM
recommended_list
INNER JOIN recommendation ON recommendation.`key` = recommended_list.recommendation
WHERE
recommendation.type = '$key' AND
recommendation.`key` = 1)  AND " . (	$key=="artist" ?  "artist.`name`"  :(($key=="album")?"album.`subject`":"music.`subject`")) . "Like '%$sea%'";

					}
					else{
					 $querycount="
			select * from $key where `key` not in(
SELECT
recommended_list.key_item
FROM
recommended_list
INNER JOIN recommendation ON recommendation.`key` = recommended_list.recommendation
WHERE
recommendation.type = '$key' AND
recommendation.`key` = 1 

)
";
					}
             $recordSetnum = JO_query( $querycount );
             $numpages=$recordSetnum->num_rows;
             if(($numpages%30)!=0) {$numpages=(int)($numpages/30)+1;}
             else                {$numpages=$numpages/30;}
					if(isset ($_REQUEST["sea"])){
						$sea=$_REQUEST['sea'];
						$query="
			select * from $key where `key` not in(
SELECT
recommended_list.key_item
FROM
recommended_list
INNER JOIN recommendation ON recommendation.`key` = recommended_list.recommendation
WHERE
recommendation.type = '$key' AND
recommendation.`key` = 1 ) AND " . (	$key=="artist" ?  "artist.`name`"  :(($key=="album")?"album.`subject`":"music.`subject`")) . " Like '%$sea%'  Limit ".(($page-1)*30).",30";
					
					}
					else{
                    $query="select * from $key where `key` not in(
SELECT
recommended_list.key_item
FROM
recommended_list
INNER JOIN recommendation ON recommendation.`key` = recommended_list.recommendation
WHERE
recommendation.type = '$key' AND
recommendation.`key` = 1
)
Limit ".(($page-1)*30).",30
 ";
					}
                }
                $recordSet = JO_query( $query );
                if($recordSet->num_rows) while ($record = $recordSet -> fetch_array( )){
				
                    ?>
      <span id="tr_<?=$record["key"]?>">
      <table style="width: 100%;margin: 0px 4px 0px 0px;">
        <tr >
          <td id="td_<?=$record["key"]?>"><?=($key=="music" || $key=="album")?$record['subject']:$record['name']?></td>
          <td style="width: 12px"><a href="#i" onclick='addtop("<?=$record["key"]?>");'><img src="JO_80_image/add_top.png" class="JO_icon" alt="Add To Top Music" title="Add To Top Music"/></a></td>
        </tr>
      </table>
      </span>
      <?
                }
                ?>
      </span>
      <table style="width: 100%;margin: 0px 4px 0px 0px;">
        <tr >
          <td style="text-align: center;border-bottom: 0px"><?

                if($numpages>1){
                    if($page>2){
                    ?>
            <a href="#" onclick="sendurl('1');">◀</a>
            <?
                    }
                    if($page>1){
                    ?>
            <a href="#" onclick="sendurl('<?=($page-1)?>');">◁</a>
            <?
                    }
                    if($page-4>1) echo "...";
                    for($i=$page-4;$i<=($page-1);$i++){
                        if($i>0){
                        ?>
            <a href="#" onclick="sendurl('<?=$i?>');">
            <?=$i?>
            </a>
            <?
                        }
                    }
                    echo  "<b style='color=#ffffff;'><u>".$page."</u></b>";
                    for($i=$page+1;$i<=($page+4);$i++){
                        if($i<=$numpages){
                        ?>
            <a href="#" onclick="sendurl('<?=$i?>');">
            <?=$i?>
            </a>
            <?
                        }
                    }
                    if($page+4<$numpages) echo "...";
                    if($page<$numpages){
                    ?>
            <a href="#" onclick="sendurl('<?=($page+1) ?>');">▷</a>
            <?
                    }
                    if($page<($numpages-1)){
                    ?>
            <a href="#" onclick="sendurl('<?=$numpages ?>');">▶</a>
            <?
                    }

                }
                 ?></td>
        </tr>
      </table>
      <div id ="kqkq"></div>
    </div>
  </div>
  <script type="text/javascript">
    function addtop(id){
        
        var contenttop="<span id='trtop_"+id+"'>";
        contenttop+="<table  style='width: 100%;margin: 0px 4px 0px 0px;'>";
        contenttop+="<tr >";
		contenttop+="<input type='hidden' value='"+id+"' name='key[]'>"
		contenttop+="<td style='width: 50px' id='tdtopid_"+id+"'>"+id+"</td>";
        contenttop+="<td id='tdtop_"+id+"'>"+document.getElementById("td_"+id).innerHTML+"</td>";
		contenttop+="<td style='width: 12px'><a href='#i' onclick='topup("+id+");'><img src='JO_80_image/JO_27_icon_up_on.png' class='JO_icon' alt='Up' title='Up'/></a></td>";
        contenttop+="<td style='width: 12px'><a href='#i' onclick='topdown("+id+");'><img src='JO_80_image/JO_28_icon_down_on.png' class='JO_icon' alt='Down' title='Down'/></a></td>";
        contenttop+="<td style='width: 12px'><a href='#i' onclick='removetop("+id+");'><img src='JO_80_image/JO_23_icon_delete_on.png' class='JO_icon' alt='Remove' title='Remove'/></a></td>";
        contenttop+="</tr>";
        contenttop+="</table>";
        contenttop+="</span>";
        document.getElementById("tb_top_music").innerHTML+=contenttop;

        //alert(document.getElementById("tb_top_music").innerHTML);
        var del = document.getElementById("tr_"+id);
        del.parentNode.removeChild(del);
        musics[n]=id;
        n++;
        //document.getElementById("kqkq").innerHTML=musics;
        create_top();
    }
    function removetop(id){
        
        var contentmusic="<span id='tr_"+id+"'>";
        contentmusic+="<table style='width: 100%;margin: 0px 4px 0px 0px;' >";
        contentmusic+="<tr ><td id='td_"+id+"'>"+document.getElementById("tdtop_"+id).innerHTML+"</td>";
        contentmusic+="<td style='width:12px'><a href='#i' onclick='addtop("+id+");'><img src='JO_80_image/add_top.png' class='JO_icon' alt='Add To Top Music' title='Add To Top Music'/></a></td>";
        contentmusic+="</tr>";
        contentmusic+="</table>";
        contentmusic+="</span>";
        document.getElementById("tb_music").innerHTML+=contentmusic;
        var del=document.getElementById("trtop_"+id);
        del.parentNode.removeChild(del);
        for(var i=0;i<musics.length;i++){
            if(musics[i]==id){
                musics[i]=undefined;
                create_top();
                break;
            }
        }
        
    }
    function topup(id){

        //alert(document.getElementById("trtop_"+id).id+":id");
        for(var i=0;i<musics.length;i++){
            if(musics[i]==id && i>0){
                var j=1;
                while(musics[i-j]==undefined && i-j>=0){
                    j++;
                }

                var contenttmp=document.getElementById("trtop_"+musics[i]).innerHTML;
//                var contenttmp2=document.getElementById("trtop_"+musics[i-j]);
//                var pct=contenttmp.parentNode;
//                pct.replaceChild(contenttmp,contenttmp2);
//                pct.replaceChild(contenttmp2,contenttmp);
                document.getElementById("trtop_"+musics[i]).innerHTML=document.getElementById("trtop_"+musics[i-j]).innerHTML;;
                document.getElementById("trtop_"+musics[i-j]).innerHTML=contenttmp;

                var idtmptr=document.getElementById("trtop_"+musics[i]).id;
                var idtmptr2=document.getElementById("trtop_"+musics[i-j]).id;
                document.getElementById("trtop_"+musics[i]).id=document.getElementById("trtop_"+musics[i]).id+"abc123";
                document.getElementById("trtop_"+musics[i-j]).id=idtmptr;
                document.getElementById("trtop_"+musics[i]+"abc123").id=idtmptr2

                //hoan vi 2 element
                var tmp=musics[i];
                musics[i]=musics[i-j];
                musics[i-j]=tmp;

                //document.getElementById("kqkq").innerHTML=musics;
                create_top();
                //alert(musics);
                break;
            }
        }
        
    }
    function topdown(id){
	
        //alert(document.getElementById("trtop_"+id).id+":id");
        for(var i=0;i<musics.length;i++){
            if(musics[i]==id){
                var j=1;
                while(musics[i+j]==undefined && i+j<musics.length){

                    j++;
                }
                if(musics[i+j]==undefined){ return;}
                //alert(musics[i]+":"+musics[i+j]);
               
 

                var contenttmpdown=document.getElementById("trtop_"+musics[i]).innerHTML;
                document.getElementById("trtop_"+musics[i]).innerHTML=document.getElementById("trtop_"+musics[i+j]).innerHTML;
                document.getElementById("trtop_"+musics[i+j]).innerHTML=contenttmpdown;
                //alert(document.getElementById("trtop_"+musics[i+j]).id);

                var idtmptr=document.getElementById("trtop_"+musics[i]).id;
                var idtmptr2=document.getElementById("trtop_"+musics[i+j]).id;
                document.getElementById("trtop_"+musics[i]).id=document.getElementById("trtop_"+musics[i]).id+"abc123";
                document.getElementById("trtop_"+musics[i+j]).id=idtmptr;
                document.getElementById("trtop_"+musics[i]+"abc123").id=idtmptr2;
                
                //hoan vi 2 element
                var tmp=musics[i];
                musics[i]=musics[i+j];
                musics[i+j]=tmp;

                //document.getElementById("kqkq").innerHTML=musics;
                create_top();
                //alert(musics);
                break;
            }
        }

    }
    function save(){
        
        var top=";";
        for(var i=0;i<musics.length;i++){
            if(musics[i]!=undefined){
                top+=musics[i]+";";
            }
        }
        //if(top==";"){return;}
		//alert(top);
        //
		//document.getElementById( "JO_integration_asynchronous" ).src = "JO_05_execution.php?JO_query=SaveTopArtist&top="+top+"&artist=<?=$artistid?>";
		location = "JO_05_execution.php?JO_query=SaveTop&top="+top+"&recommendation=<?=$_REQUEST['key']?>";
    }
    function create_top()
    {
        var ntop=1;
        for(var i=0;i<musics.length;i++){
            if(musics[i]!=undefined){
                document.getElementById("tdtopid_"+musics[i]).innerHTML=ntop;
                ntop++;
            }
        }
    }
    function changeartist(c){
        location="JO_00_integration.php?JO_unit=new_topartist&artistid="+c.value+"&searchkey=<?=urlencode($searchkeyartist)?>";
    }
    function searchfocus(v){
        if(v.value=="<?=$JO["JO_searchid"]?>")
            v.value="";
    }
    function searchblur(v){
        var s=v.value;
        //Cắt bỏ những khoảng trắng đầu chuỗi
        while(s.charCodeAt(0)==32)
            s=s.substring(1);
        //Cắt bỏ những khoảng trắng cuối chuỗi
        while(s.charCodeAt(s.length-1)==32)
            s=s.substring(0,s.length-1);
        if(s=="")
            v.value="<?=$JO["JO_searchid"]?>";
        else
            v.value=s;
    }
    window.onload = function() {
        document.getElementById("txtsearchartist").focus();
    }
    function searchKeyPress(e,str)
    {
		
        // look for window.event in case event isn't passed in
        if (window.event) { e = window.event; }
        if (e.keyCode == 13)
        {
            

                location=("JO_00_integration.php?JO_unit=68_managing_Top_Rbt1&type=<?=$key?>&key=<?=$recommendation?>&sea="+str);
            

        }
    }
    function sendurl(p){
            <?
            if($searchkeyartist==$JO["JO_searchid"]){
              ?>
                location=("JO_00_integration.php?JO_unit=68_managing_Top_Rbt1&Page="+p+"&type=<?=$key?>"+"&key=<?=$recommendation?>");
                <?
            }else{
                ?>
                location=("JO_00_integration.php?JO_unit=68_managing_Top_Rbt1&Page="+p++"&type=<?=$key?>"+"&key=<?=$recommendation?>"+"&searchkey=<?=urlencode($searchkeyartist)?>");
                <?
            }
            ?>
            
    }
          function sendurl2(p){
            <?
            if($searchkeyartist==$JO["JO_searchid"]){
              ?>
                location=("JO_00_integration.php?JO_unit=new_topartist&Page=<?=$page?>&artistid="+p);
                <?
            }else{
                ?>
                        location=("JO_00_integration.php?JO_unit=new_topartist&Page=<?=$page?>&artistid="+p+"&searchkey=<?=  urlencode($searchkeyartist)?>");
                <?
            }
            ?>

    }
 </script> 
</div>
